package com.chen.thread.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chen.thread.dto.User2;
import com.chen.thread.dto.User3;
import com.chen.thread.service.User2Service;
import com.chen.thread.service.User3Service;
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Api("复制表")
@RestController
@Slf4j
public class User3Controller {


    @Resource
    private User3Service user3Service;

    @Resource
    private User2Service user2Service;

    @ApiOperation("普通复制，sql是一条一条插入的，需要将获取到的数据，先转成Use3，一条一条的插入到User3中")
    @PostMapping("/copy")
    public Object insert(User3 user3) {
        JSONObject jsonObject = new JSONObject();
//        try{
        List<User2> lists = user2Service.selectInfo();
        long starTime = System.currentTimeMillis();
        for (User2 user2 : lists) {
            user3.setUsername(user2.getUsername());
            user3.setPassword(user2.getPassword());
            user3Service.insert(user3);
        }
        long endTime = System.currentTimeMillis();
        String time = String.valueOf((endTime - starTime) / 1000.0);
//        if (flag) {
        jsonObject.put("code", 1);
        jsonObject.put("message", "耗时" + time + "秒");
        return jsonObject;
//        }
//        jsonObject.put("code", 0);
//        jsonObject.put("message", "插入失败");
//        return jsonObject;
//        }catch (Exception e){
//            e.printStackTrace();
//        }
    }
    @ApiOperation("插入数据，sql是批量插入的，传入一个list参数，直接将user2和user3共同的字段进行操作")
    @PostMapping("/copyForeach")
    public Object insertForeach(User3 user3){
        JSONObject jsonObject = new JSONObject();
        long starTime = System.currentTimeMillis();
        List<User2> lists = user2Service.selectInfo();
        user3Service.insertForeach(lists);
        long endTime = System.currentTimeMillis();
        String time = String.valueOf((endTime - starTime) / 1000.0);
        jsonObject.put("code", 1);
        jsonObject.put("message", "耗时" + time + "秒");
        return jsonObject;
    }
    @ApiOperation("删除user3中所有数据")
    @PostMapping("/delete")
    public void delete() {
        user3Service.delete();
    }
    @ApiOperation("使用多线程进行复制数据")
    @PostMapping("/copyThread")
    public Object insertThread(User3 user3) throws InterruptedException {
//        User3 user3;
        MyThread myThread;

        ExecutorService pool = Executors.newFixedThreadPool(2);
        final CountDownLatch countDownLatch = new CountDownLatch(2);  //计数器2 也设置成两个线程
        JSONObject jsonObject = new JSONObject();
        List<User2> lists = user2Service.selectInfo();
        long starTime = System.currentTimeMillis();
        for (User2 user2 : lists) {
            user3 = new User3();
            user3.setUsername(user2.getUsername());
            user3.setPassword(user2.getPassword());
            myThread = new MyThread(countDownLatch, user3);
            pool.execute(myThread);
        }

        countDownLatch.await();

        log.info("------------------" + Thread.currentThread().getName() + "-------123--------");

        pool.shutdown();
        while(true){
            if(pool.isTerminated()){
                log.info("所有的子线程都结束了");
                break;
            }
        }
//        while(!pool.isShutdown()){
//
//        }
        long endTime = System.currentTimeMillis();
        String time = String.valueOf((endTime - starTime));
        log.info("------------------看看啥时候记的结束时间-------123--------");
        jsonObject.put("code", 1);
        jsonObject.put("message", "耗时" + time + "毫秒");
        return jsonObject;
    }

    class MyThread implements Runnable {
        private CountDownLatch countDownLatch;
        private User3 user3;

        public MyThread(CountDownLatch countDownLatch, User3 user3) {
            this.countDownLatch = countDownLatch;
            this.user3 = user3;
        }

        @Override
        public void run() {
            try {
//                synchronized (countDownLatch) {
//                    Thread.sleep(3000);
                log.info("Thread name:" + Thread.currentThread().getName() + " is starting............");
                user3Service.insert(user3);
                countDownLatch.countDown();
//                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

//    public static void main(String[] args) {
//        User3 user3 = new User3();
//        CountDownLatch countDownLatch = new CountDownLatch(2);
//        MyThread thread = new MyThread(countDownLatch, user3);
//        new Thread(thread).start();
//        System.out.println("-----------------------");
//    }


}
